package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.Constant;
import com.example.demo.dao.SettingRepository;
import com.example.demo.model.entity.SettingModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = Constant.API_VERSION + "/share")
public class ShareController {

    @Autowired
    private SettingRepository settingRepository;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject share() throws Exception {
        SettingModel setting = settingRepository.findBySettingType(1);
        JSONObject result = new JSONObject();
        result.put("url", setting.getSettingConfig());
        result.put("id", setting.getSettingId());
        return result;
    }


    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public void updateShare(@RequestBody JSONObject request) throws Exception {
        SettingModel setting = settingRepository.findOne(request.getString("id"));
        setting.setSettingConfig(request.getString("url"));
        settingRepository.save(setting);
    }


}
